package com.hp.bon.sgw.stdbss30.bizSlotOutside;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.stdbss30.BizSlot;
import com.hp.bon.sgw.stdbss30.utils.HttpClientUtils;
import com.hp.bon.sgw.stdbss30.common.BizConsts;
import com.hp.bon.sgw.stdbss30.common.BizParam;
import com.hp.bon.sgw.stdbss30.common.BizResult;
import com.hp.bon.sgw.wh.service.UserResourceConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QryBalOweBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(QryBalOweBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();

        try {
            String areaCode = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Area_Code");
            String destinationAccount = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Destination_Account");

            JSONObject queryBalanceOperAttrStruct = new JSONObject();
            queryBalanceOperAttrStruct.put("operServiceId", "-1");
            queryBalanceOperAttrStruct.put("lanId", "8420100");
            queryBalanceOperAttrStruct.put("operPost", "0");
            queryBalanceOperAttrStruct.put("operOrgId", "0");
            queryBalanceOperAttrStruct.put("staffId", "0");
            queryBalanceOperAttrStruct.put("operTime", "0");

            JSONObject svcObjectStruct = new JSONObject();
            svcObjectStruct.put("objValue", areaCode + destinationAccount);
            svcObjectStruct.put("objAttr", "99");
            svcObjectStruct.put("objType", "3");
            svcObjectStruct.put("dataArea", "0");

            //构建请求条件
            JSONObject queryBalanceJsonObject = new JSONObject();
            queryBalanceJsonObject.put("operAttrStruct", queryBalanceOperAttrStruct);
            queryBalanceJsonObject.put("queryFlag", "0");
            queryBalanceJsonObject.put("queryItemType", "1");
            queryBalanceJsonObject.put("areaCode", areaCode);
            queryBalanceJsonObject.put("svcObjectStruct", svcObjectStruct);

            StringBuilder queryBalanceMsgBodyStr = new StringBuilder();

            //发送请求
            String queryBalanceUrl = UserResourceConfigService.getValue("QueryBalance.jf3");
            String billQueryOweUrl = UserResourceConfigService.getValue("BillQueryOwe.jf3");

            queryBalanceMsgBodyStr.append(queryBalanceJsonObject.toJSONString());
            String queryBalanceRespStr = HttpClientUtils.postHttpStr(queryBalanceMsgBodyStr.toString(), queryBalanceUrl);

            if (queryBalanceRespStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }


            JSONObject qryBillSvcObjectStruct = new JSONObject();
            qryBillSvcObjectStruct.put("operServiceId", "-1");
            qryBillSvcObjectStruct.put("lanId", "8420100");
            qryBillSvcObjectStruct.put("operPost", "0");
            qryBillSvcObjectStruct.put("operOrgId", "0");
            qryBillSvcObjectStruct.put("staffId", "0");
            qryBillSvcObjectStruct.put("operTime", "0");
            //构建请求条件
            JSONObject qryBillJsonObject = new JSONObject();
            qryBillJsonObject.put("operAttrStruct", qryBillSvcObjectStruct);
            qryBillJsonObject.put("billQueryType", "2");
            qryBillJsonObject.put("destinationAccount", areaCode + destinationAccount);
            qryBillJsonObject.put("destinationAttr", "99");
            qryBillJsonObject.put("queryFlag", "0");
            qryBillJsonObject.put("feeQueryFlag", "0");
            qryBillJsonObject.put("balanceQueryFlag", "1");

            StringBuilder qryBillMsgBodyStr = new StringBuilder();

            //发送请求
            qryBillMsgBodyStr.append(qryBillJsonObject.toJSONString());

            _log.info("request body is {}", qryBillMsgBodyStr.toString().toString());
            _log.info("webserviceUrl is {}", billQueryOweUrl);
            String qryBillRespStr = HttpClientUtils.postHttpStr(qryBillMsgBodyStr.toString(), billQueryOweUrl);
            _log.info("response is {}", qryBillRespStr);

            //解析json 拼接报文
            JSONObject queryBalanceJson = JSON.parseObject(queryBalanceRespStr);
            JSONObject qryBillJson = JSON.parseObject(qryBillRespStr);
            StringBuffer buffer = new StringBuffer();
            jsonToXmlStr(xmlMessage, queryBalanceJson, qryBillJson, buffer);


            bizResult.setHttpBody(buffer.toString());
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(XmlMessage xmlMessage, JSONObject queryBalanceJson, JSONObject qryBillJson, StringBuffer buffer) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        buffer.append("<SOAP-ENV:Header/>");
        buffer.append("<SOAP-ENV:Body>");
        buffer.append("<ns3:SgwQueryAck xmlns:ns3=\"http://www.chinatelecom.cn/SGW/SOAP\">");
        buffer.append("<MID>" + xmlMessage.getMID() + "</MID>");
        buffer.append("<REPTAG>" + xmlMessage.getREPTAG() + "</REPTAG>");
        buffer.append("<RTT>" + xmlMessage.getRTT() + "</RTT>");
        buffer.append("<SIG>" + xmlMessage.getSIG() + "</SIG>");
        buffer.append("<RC>" + xmlMessage.getRC() + "</RC>");
        buffer.append("<DS>" + xmlMessage.getDS() + "</DS>");
        buffer.append("<DATA><![CDATA[<Service_Information>");

        if (String.valueOf(queryBalanceJson.get("resultCode")).equals("0") && String.valueOf(qryBillJson.get("resultCode")).equals("0")) {
            buffer.append("<Result>0</Result>");
        } else if (!String.valueOf(queryBalanceJson.get("resultCode")).equals("0")) {
            buffer.append("<Result>" + (queryBalanceJson.get("resultCode") == null ? "" : queryBalanceJson.get("resultCode")) + "</Result>");
        } else if (!String.valueOf(qryBillJson.get("resultCode")).equals("0")) {
            buffer.append("<Result>" + (qryBillJson.get("resultCode") == null ? "" : qryBillJson.get("resultCode")) + "</Result>");
        }

        buffer.append("<Acct_Item_Charge>" + (qryBillJson.get("shouldCharge") == null ? "" : qryBillJson.get("shouldCharge")) + "</Acct_Item_Charge>");
        buffer.append("<Valid_Balance>" + (queryBalanceJson.get("realBalance") == null ? "" : queryBalanceJson.get("realBalance")) + "</Valid_Balance>");

        buffer.append("</Service_Information>]]></DATA>");
        buffer.append("</ns3:SgwQueryAck>");
        buffer.append("</SOAP-ENV:Body>");
        buffer.append("</SOAP-ENV:Envelope>");
    }

}
